package com.ruoyi.classai.utils.aliyunUtil;

import com.aliyun.facebody20191230.*;
import com.aliyun.facebody20191230.models.ListFaceDbsRequest;
import com.aliyun.tea.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static com.aliyun.teaconsole.Client.log;
import static com.aliyun.teautil.Common.assertAsString;
import static com.aliyun.teautil.Common.toJSONString;

@Component
public class AliyunFaceUtil {

    @Value("${aliyun.url}")
    public String url;

    @Value("${aliyun.accessKey}")
    public String accessKey;

    @Value("${aliyun.secretKey}")
    public String secretKey;

    public Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKey)
                .setAccessKeySecret(secretKey);
        config.endpoint = url;
        return new Client(config);
    }

    //查看数据库列表ListFaceDb
    public void ListFaceDbs() throws Exception {
        Client client = this.createClient();
        ListFaceDbsRequest listFaceDbsRequest = new ListFaceDbsRequest();
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            Object resp = client.listFaceDbsWithOptions(listFaceDbsRequest, runtime);
            log(toJSONString(resp));
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            assertAsString(error.message);
        }
    }

}
